﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using OracleDataToRedis.Domain;
using OracleDataToRedis.Utils;

namespace OracleDataToRedis.Coders.UserControls
{
    public class UcPagingCoder
    {
        public static void Write()
        {
            string path = Path.Combine(BaseParams.UserControlsPath, "Utility");
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }

            #region 创建类UcPaging文件
            string className = Path.Combine(path, "UcPaging.cs");
            FileStream file = new FileStream(className, FileMode.Create);
            StreamWriter sw = new StreamWriter(file, Encoding.UTF8);
            CommentsCoder.CreateCsComments("分页用户控件", sw);
            sw.WriteLine("using System;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using System.ComponentModel;");
            sw.WriteLine("using System.Drawing;");
            sw.WriteLine("using System.Data;");
            sw.WriteLine("using System.Text;");
            sw.WriteLine("using System.Windows.Forms;");
            sw.WriteLine("");
            sw.WriteLine("using " + BaseParams.UtilityNameSpace + ";");
            sw.WriteLine("");
            sw.WriteLine("namespace " + BaseParams.UserControlsNameSpace + ".Utility");
            sw.WriteLine("{");
            sw.WriteLine("    public delegate void EventPagingChanged(object sender, MyPagingEventArgs e);");
            sw.WriteLine("");
            sw.WriteLine("    public partial class UcPaging : UserControl");
            sw.WriteLine("    {");
            sw.WriteLine("        public UcPaging()");
            sw.WriteLine("        {");
            sw.WriteLine("            InitializeComponent();");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        private Paging _dataPaging;");
            sw.WriteLine("");
            sw.WriteLine("        #region PagingChanged 事件");
            sw.WriteLine("        public event EventPagingChanged PagingChanged;");
            sw.WriteLine("        protected void OnPagingChanged(Paging paging)");
            sw.WriteLine("        {");
            sw.WriteLine("            //判断事件是否为空");
            sw.WriteLine("            if (PagingChanged != null)");
            sw.WriteLine("            {");
            sw.WriteLine("                //执行事件");
            sw.WriteLine("                PagingChanged(this, new MyPagingEventArgs(paging));");
            sw.WriteLine("");
            sw.WriteLine("                PagingReflash();");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        public void PagingReflash()");
            sw.WriteLine("        {");
            sw.WriteLine("            this.txtCurrent.Text = this.DataPaging.CurrentPage.ToString();");
            sw.WriteLine("            this.lblTotoal.Text = this.DataPaging.TotalPage.ToString();");
            sw.WriteLine("            this.lblInfo.Text = \"每页：\" + this.DataPaging.PageSize + \"项，总计：\" + this.DataPaging.RowCount + \"项\";");
            sw.WriteLine("");
            sw.WriteLine("            this.btnFirst.Enabled = !(this.DataPaging.CurrentPage == 1);");
            sw.WriteLine("            this.btnPrev.Enabled = !(this.DataPaging.CurrentPage == 1);");
            sw.WriteLine("");
            sw.WriteLine("            this.btnLast.Enabled = !(this.DataPaging.CurrentPage == this.DataPaging.TotalPage);");
            sw.WriteLine("            this.btnNext.Enabled = !(this.DataPaging.CurrentPage == this.DataPaging.TotalPage);");
            sw.WriteLine("        }");
            sw.WriteLine("        #endregion");
            sw.WriteLine("");
            sw.WriteLine("        public Paging DataPaging");
            sw.WriteLine("        {");
            sw.WriteLine("            get");
            sw.WriteLine("            {");
            sw.WriteLine("                if (this._dataPaging == null) this._dataPaging = new Paging();");
            sw.WriteLine("                return this._dataPaging;");
            sw.WriteLine("            }");
            sw.WriteLine("            set");
            sw.WriteLine("            {");
            sw.WriteLine("                this._dataPaging = value;");
            sw.WriteLine("                PagingReflash();");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("        public long PageTotal");
            sw.WriteLine("        {");
            sw.WriteLine("            get");
            sw.WriteLine("            {");
            sw.WriteLine("                return this.DataPaging.TotalPage;");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        protected void btnFirst_Click(object sender, EventArgs e)");
            sw.WriteLine("        {");
            sw.WriteLine("            this.DataPaging.CurrentPage = 1;");
            sw.WriteLine("            this.OnPagingChanged(this.DataPaging);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        protected void btnPrev_Click(object sender, EventArgs e)");
            sw.WriteLine("        {");
            sw.WriteLine("            if (this.DataPaging.CurrentPage != 1)");
            sw.WriteLine("            {");
            sw.WriteLine("                this.DataPaging.CurrentPage += -1;");
            sw.WriteLine("            }");
            sw.WriteLine("            this.OnPagingChanged(this.DataPaging);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        protected void btnNext_Click(object sender, EventArgs e)");
            sw.WriteLine("        {");
            sw.WriteLine("            if (this.DataPaging.CurrentPage != this.DataPaging.RowCount)");
            sw.WriteLine("            {");
            sw.WriteLine("                this.DataPaging.CurrentPage += 1;");
            sw.WriteLine("            }");
            sw.WriteLine("            this.OnPagingChanged(this.DataPaging);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        protected void btnLast_Click(object sender, EventArgs e)");
            sw.WriteLine("        {");
            sw.WriteLine("            this.DataPaging.CurrentPage = this.DataPaging.TotalPage;");
            sw.WriteLine("            this.OnPagingChanged(this.DataPaging);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        private void txtCurrent_KeyDown(object sender, KeyEventArgs e)");
            sw.WriteLine("        {");
            sw.WriteLine("            if (e.KeyCode == Keys.Enter)");
            sw.WriteLine("            {");
            sw.WriteLine("                try");
            sw.WriteLine("                {");
            sw.WriteLine("                    int cp = int.Parse(this.txtCurrent.Text);");
            sw.WriteLine("                    if (cp > 0 && cp <= this.DataPaging.TotalPage)");
            sw.WriteLine("                    {");
            sw.WriteLine("                        if (this.DataPaging.CurrentPage != cp)");
            sw.WriteLine("                        {");
            sw.WriteLine("                            this.DataPaging.CurrentPage = cp;");
            sw.WriteLine("                            this.OnPagingChanged(this.DataPaging);");
            sw.WriteLine("                        }");
            sw.WriteLine("                    }");
            sw.WriteLine("                    else");
            sw.WriteLine("                    {");
            sw.WriteLine("                        MessageBox.Show(\"当前页码只能输入小于或者等于总页数的正整数！\", \"错误\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            sw.WriteLine("                    }");
            sw.WriteLine("                }");
            sw.WriteLine("                catch");
            sw.WriteLine("                {");
            sw.WriteLine("                    MessageBox.Show(\"当前页码只能输入小于或者等于总页数的正整数！\", \"错误\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            sw.WriteLine("                }");
            sw.WriteLine("                this.txtCurrent.SelectAll();");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("    }");
            sw.WriteLine("");
            sw.WriteLine("    /// <summary>");
            sw.WriteLine("    /// MyPagingEventArgs 的摘要说明。");
            sw.WriteLine("    /// </summary>");
            sw.WriteLine("    public class MyPagingEventArgs");
            sw.WriteLine("    {");
            sw.WriteLine("        private Paging _paging;");
            sw.WriteLine("");
            sw.WriteLine("        // 构造函数");
            sw.WriteLine("        public MyPagingEventArgs(Paging paging)");
            sw.WriteLine("        {");
            sw.WriteLine("            this._paging = paging;");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        // 声明只读属性, 返回分页对象");
            sw.WriteLine("        public Paging Paging");
            sw.WriteLine("        {");
            sw.WriteLine("            get");
            sw.WriteLine("            {");
            sw.WriteLine("                return this._paging;");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("    }");
            sw.WriteLine("}");
            sw.Close();
            file.Close();
            #endregion

            #region 创建类UcPaging文件
            className = Path.Combine(path, "UcPaging.Designer.cs");
            file = new FileStream(className, FileMode.Create);
            sw = new StreamWriter(file, Encoding.UTF8);
            sw.WriteLine("namespace " + BaseParams.UserControlsNameSpace + ".Utility");
            sw.WriteLine("{");
            sw.WriteLine("    partial class UcPaging");
            sw.WriteLine("    {");
            sw.WriteLine("        /// <summary> ");
            sw.WriteLine("        /// 必需的设计器变量。");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        private System.ComponentModel.IContainer components = null;");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary> ");
            sw.WriteLine("        /// 清理所有正在使用的资源。");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"disposing\">如果应释放托管资源，为 true；否则为 false。</param>");
            sw.WriteLine("        protected override void Dispose(bool disposing)");
            sw.WriteLine("        {");
            sw.WriteLine("            if (disposing && (components != null))");
            sw.WriteLine("            {");
            sw.WriteLine("                components.Dispose();");
            sw.WriteLine("            }");
            sw.WriteLine("            base.Dispose(disposing);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        #region 组件设计器生成的代码");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary> ");
            sw.WriteLine("        /// 设计器支持所需的方法 - 不要");
            sw.WriteLine("        /// 使用代码编辑器修改此方法的内容。");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        private void InitializeComponent()");
            sw.WriteLine("        {");
            sw.WriteLine("            this.btnFirst = new " + BaseParams.ControlsNameSpace + ".MyButton();");
            sw.WriteLine("            this.btnPrev = new " + BaseParams.ControlsNameSpace + ".MyButton();");
            sw.WriteLine("            this.btnNext = new " + BaseParams.ControlsNameSpace + ".MyButton();");
            sw.WriteLine("            this.btnLast = new " + BaseParams.ControlsNameSpace + ".MyButton();");
            sw.WriteLine("            this.myLabel1 = new " + BaseParams.ControlsNameSpace + ".MyLabel();");
            sw.WriteLine("            this.lblTotoal = new " + BaseParams.ControlsNameSpace + ".MyLabel();");
            sw.WriteLine("            this.lblInfo = new " + BaseParams.ControlsNameSpace + ".MyLabel();");
            sw.WriteLine("            this.txtCurrent = new System.Windows.Forms.TextBox();");
            sw.WriteLine("            this.SuspendLayout();");
            sw.WriteLine("            // ");
            sw.WriteLine("            // btnFirst");
            sw.WriteLine("            // ");
            sw.WriteLine("            this.btnFirst.Location = new System.Drawing.Point(4, 6);");
            sw.WriteLine("            this.btnFirst.Name = \"btnFirst\";");
            sw.WriteLine("            this.btnFirst.Size = new System.Drawing.Size(54, 23);");
            sw.WriteLine("            this.btnFirst.TabIndex = 1;");
            sw.WriteLine("            this.btnFirst.Text = \"首页\";");
            sw.WriteLine("            this.btnFirst.UseVisualStyleBackColor = true;");
            sw.WriteLine("            this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);");
            sw.WriteLine("            // ");
            sw.WriteLine("            // btnPrev");
            sw.WriteLine("            // ");
            sw.WriteLine("            this.btnPrev.Location = new System.Drawing.Point(64, 6);");
            sw.WriteLine("            this.btnPrev.Name = \"btnPrev\";");
            sw.WriteLine("            this.btnPrev.Size = new System.Drawing.Size(54, 23);");
            sw.WriteLine("            this.btnPrev.TabIndex = 1;");
            sw.WriteLine("            this.btnPrev.Text = \"上一页\";");
            sw.WriteLine("            this.btnPrev.UseVisualStyleBackColor = true;");
            sw.WriteLine("            this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);");
            sw.WriteLine("            // ");
            sw.WriteLine("            // btnNext");
            sw.WriteLine("            // ");
            sw.WriteLine("            this.btnNext.Location = new System.Drawing.Point(207, 6);");
            sw.WriteLine("            this.btnNext.Name = \"btnNext\";");
            sw.WriteLine("            this.btnNext.Size = new System.Drawing.Size(54, 23);");
            sw.WriteLine("            this.btnNext.TabIndex = 1;");
            sw.WriteLine("            this.btnNext.Text = \"下一页\";");
            sw.WriteLine("            this.btnNext.UseVisualStyleBackColor = true;");
            sw.WriteLine("            this.btnNext.Click += new System.EventHandler(this.btnNext_Click);");
            sw.WriteLine("            // ");
            sw.WriteLine("            // btnLast");
            sw.WriteLine("            // ");
            sw.WriteLine("            this.btnLast.Location = new System.Drawing.Point(267, 6);");
            sw.WriteLine("            this.btnLast.Name = \"btnLast\";");
            sw.WriteLine("            this.btnLast.Size = new System.Drawing.Size(54, 23);");
            sw.WriteLine("            this.btnLast.TabIndex = 1;");
            sw.WriteLine("            this.btnLast.Text = \"末页\";");
            sw.WriteLine("            this.btnLast.UseVisualStyleBackColor = true;");
            sw.WriteLine("            this.btnLast.Click += new System.EventHandler(this.btnLast_Click);");
            sw.WriteLine("            // ");
            sw.WriteLine("            // myLabel1");
            sw.WriteLine("            // ");
            sw.WriteLine("            this.myLabel1.AutoSize = true;");
            sw.WriteLine("            this.myLabel1.Location = new System.Drawing.Point(158, 11);");
            sw.WriteLine("            this.myLabel1.Name = \"myLabel1\";");
            sw.WriteLine("            this.myLabel1.Size = new System.Drawing.Size(11, 12);");
            sw.WriteLine("            this.myLabel1.TabIndex = 5;");
            sw.WriteLine("            this.myLabel1.Text = \"/\";");
            sw.WriteLine("            // ");
            sw.WriteLine("            // lblTotoal");
            sw.WriteLine("            // ");
            sw.WriteLine("            this.lblTotoal.AutoSize = true;");
            sw.WriteLine("            this.lblTotoal.Location = new System.Drawing.Point(173, 11);");
            sw.WriteLine("            this.lblTotoal.Name = \"lblTotoal\";");
            sw.WriteLine("            this.lblTotoal.Size = new System.Drawing.Size(11, 12);");
            sw.WriteLine("            this.lblTotoal.TabIndex = 6;");
            sw.WriteLine("            this.lblTotoal.Text = \"1\";");
            sw.WriteLine("            // ");
            sw.WriteLine("            // lblInfo");
            sw.WriteLine("            // ");
            sw.WriteLine("            this.lblInfo.AutoSize = true;");
            sw.WriteLine("            this.lblInfo.Location = new System.Drawing.Point(336, 11);");
            sw.WriteLine("            this.lblInfo.Name = \"lblInfo\";");
            sw.WriteLine("            this.lblInfo.Size = new System.Drawing.Size(53, 12);");
            sw.WriteLine("            this.lblInfo.TabIndex = 7;");
            sw.WriteLine("            this.lblInfo.Text = \"myLabel2\";");
            sw.WriteLine("            // ");
            sw.WriteLine("            // txtCurrent");
            sw.WriteLine("            // ");
            sw.WriteLine("            this.txtCurrent.Location = new System.Drawing.Point(121, 7);");
            sw.WriteLine("            this.txtCurrent.MaxLength = 10;");
            sw.WriteLine("            this.txtCurrent.Name = \"txtCurrent\";");
            sw.WriteLine("            this.txtCurrent.Size = new System.Drawing.Size(35, 21);");
            sw.WriteLine("            this.txtCurrent.TabIndex = 1;");
            sw.WriteLine("            this.txtCurrent.Text = \"0\";");
            sw.WriteLine("            this.txtCurrent.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;");
            sw.WriteLine("            this.txtCurrent.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtCurrent_KeyDown);");
            sw.WriteLine("            // ");
            sw.WriteLine("            // UcPaging");
            sw.WriteLine("            // ");
            sw.WriteLine("            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);");
            sw.WriteLine("            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;");
            sw.WriteLine("            this.Controls.Add(this.btnFirst);");
            sw.WriteLine("            this.Controls.Add(this.btnPrev);");
            sw.WriteLine("            this.Controls.Add(this.txtCurrent);");
            sw.WriteLine("            this.Controls.Add(this.btnNext);");
            sw.WriteLine("            this.Controls.Add(this.btnLast);");
            sw.WriteLine("            this.Controls.Add(this.lblTotoal);");
            sw.WriteLine("            this.Controls.Add(this.lblInfo);");
            sw.WriteLine("            this.Controls.Add(this.myLabel1);");
            sw.WriteLine("            this.Name = \"UcPaging\";");
            sw.WriteLine("            this.Size = new System.Drawing.Size(409, 33);");
            sw.WriteLine("            this.ResumeLayout(false);");
            sw.WriteLine("            this.PerformLayout();");
            sw.WriteLine("");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        #endregion");
            sw.WriteLine("");
            sw.WriteLine("        private " + BaseParams.ControlsNameSpace + ".MyButton btnFirst;");
            sw.WriteLine("        private " + BaseParams.ControlsNameSpace + ".MyButton btnPrev;");
            sw.WriteLine("        private " + BaseParams.ControlsNameSpace + ".MyButton btnNext;");
            sw.WriteLine("        private " + BaseParams.ControlsNameSpace + ".MyButton btnLast;");
            sw.WriteLine("        private " + BaseParams.ControlsNameSpace + ".MyLabel myLabel1;");
            sw.WriteLine("        private " + BaseParams.ControlsNameSpace + ".MyLabel lblTotoal;");
            sw.WriteLine("        private " + BaseParams.ControlsNameSpace + ".MyLabel lblInfo;");
            sw.WriteLine("        private System.Windows.Forms.TextBox txtCurrent;");
            sw.WriteLine("    }");
            sw.WriteLine("}");
            sw.Close();
            file.Close();
            #endregion
        }
    }
}
